home *** CD-ROM | disk | FTP | other *** search
/ More MacCube 1: Arcade Games / More MacCube Vol 1 Arcade Games.bin / Games⁄Arcade / Bolo / More information / Sample Code / Std Autopilot / BF_Main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-13  |  7.6 KB  |  237 lines  |  [TEXT/KAHL]

  1. // Bolo code (C) Stuart Cheshire <cheshire@cs.stanford.edu> 1987-1995.
  2. // All rights reserved. This code is owned by Stuart Cheshire and is donated
  3. // free of charge for non-commercial use. You may not use this code in any
  4. // product sold for commercial profit, except shareware priced at $25 or less.
  5.  
  6. #include <Fonts.h>            // Needed for InitFonts
  7. #include <TextEdit.h>        // Needed for TEToScrap etc.
  8. #include <ToolUtils.h>        // Needed for GetCursor
  9. #include <GestaltEqu.h>        // Needed for Gestalt
  10. #include <Folders.h>        // Needed for FindFolder
  11. #include <Resources.h>        // Needed for GetResource
  12. #include <Icons.h>            // Needed for GetIconSuite
  13. #include <Scrap.h>            // Needed for ZeroScrap
  14. #include <LoMem.h>            // Needed for CurrentA5
  15. #include <Traps.h>            // Needed for CurrentA5
  16.  
  17. #include "BrainFrame.h"
  18. #include "BF_ResourceIDs.h"
  19. #include "BF_Main.h"
  20. #include "BF_Globals.h"
  21. #include "BF_Utils.h"
  22. #include "BF_Prefs.h"
  23. #include "BF_Events.h"
  24. #include "BF_AppleEvents.h"
  25. #include "BF_IAC.h"
  26. #include "BF_Interface.h"
  27.  
  28. local Cursor waitingcursor;
  29.  
  30. // System environmental variables
  31. export Handle MyIconSuite;
  32. export short screenwidth, screenheight;
  33. export SysEnvRec sysenvirons;
  34. export short PrefsFolderWD;
  35. export Boolean swapMMUdefined    = FALSE;
  36. export Boolean hasDTManager      = FALSE;
  37. export Boolean hasGDManager      = FALSE;
  38.  
  39. export Boolean hasGestalt        = FALSE;
  40. export Boolean has32bitQD        = FALSE;
  41. export Boolean hasGWorlds        = FALSE;
  42. export Boolean hasPowerManager   = FALSE;
  43. export Boolean hasStdNBP         = FALSE;
  44. export Boolean isUNIX            = FALSE;
  45. export Boolean is32bitAddressing = FALSE;
  46. export Boolean hasVirtualMemory  = FALSE;
  47. export Boolean hasExtendedTimeMgr= FALSE;
  48. export Boolean hasCustomFile     = FALSE;
  49. export Boolean hasFSSpecCalls    = FALSE;
  50. export Boolean hasAppleEvents    = FALSE;
  51. export Boolean hasPPCToolBox     = FALSE;
  52.  
  53. // Progam state variables
  54. export Boolean frontapp            = TRUE;        // Are we the front application?
  55. export Boolean notification_posted = FALSE;
  56. export Boolean quitting            = FALSE;
  57.  
  58. local void MacInits(void)
  59.     {
  60.     EventRecord e;
  61.     CursHandle curshandle;
  62.     MaxApplZone();
  63.     InitGraf(&qd.thePort);
  64.     InitFonts();
  65.     InitWindows();
  66.     InitMenus();
  67.     TEInit();        // Initialize Textedit
  68.     TEFromScrap();    // And immediately read in the current clipboard
  69.     InitDialogs(0);
  70.     FlushEvents(everyEvent,0);
  71.     EventAvail(everyEvent, &e);             // Tell MF we want to be at the front!
  72.     curshandle = GetCursor(watchCursor);
  73.     if (curshandle && *curshandle) waitingcursor = **curshandle;
  74.     }
  75.  
  76. #ifndef _QDxDispatch
  77. #define _QDxDispatch 0xAB1D
  78. #endif
  79.  
  80. local OSErr Init_EnvironmentInfo(void)
  81.     {
  82.     SysEnvirons(1, &sysenvirons);
  83.     if (sysenvirons.systemVersion < 0x603)
  84.         { InitCursor(); StopAlert(alert_systemtoostupid, NULL); return(1); }
  85.     screenwidth  = qd.screenBits.bounds.right  - qd.screenBits.bounds.left;
  86.     screenheight = qd.screenBits.bounds.bottom - qd.screenBits.bounds.top - 20;
  87.     swapMMUdefined = TrapAvailable(_SwapMMUMode);
  88.     hasDTManager   = TrapAvailable(_DTInstall);
  89.     hasGDManager   = TrapAvailable(_QDxDispatch);
  90.     hasGestalt     = TrapAvailable(_Gestalt);
  91.     if (hasGestalt)
  92.         {
  93.         long feature;
  94.         if (Gestalt(gestaltQuickdrawVersion, &feature) == noErr)
  95.             if (feature >= gestalt32BitQD) has32bitQD = TRUE;
  96.         if (Gestalt(gestaltQuickdrawFeatures, &feature) == noErr)
  97.             if (feature & (1<<gestaltHasDeepGWorlds)) hasGWorlds = TRUE;
  98.         if (Gestalt(gestaltPowerMgrAttr, &feature) == noErr)
  99.             if (feature & 1) hasPowerManager = TRUE;
  100.         if (Gestalt(gestaltStdNBPAttr, &feature) == noErr)
  101.             if (feature & (1<<gestaltStdNBPPresent)) hasStdNBP = TRUE;
  102.         if (Gestalt(gestaltAUXVersion, &feature) == noErr) isUNIX = TRUE;
  103.         if (Gestalt(gestaltAddressingModeAttr, &feature) == noErr)
  104.             if (feature & (1<<gestalt32BitAddressing)) is32bitAddressing = TRUE;
  105.         if (Gestalt(gestaltVMAttr, &feature) == noErr)
  106.             if (feature & (1<<gestaltVMPresent)) hasVirtualMemory = TRUE;
  107.         if (Gestalt(gestaltTimeMgrVersion, &feature) == noErr)
  108.             if (feature >= gestaltExtendedTimeMgr) hasExtendedTimeMgr = TRUE;
  109.         if (Gestalt(gestaltStandardFileAttr, &feature) == noErr)
  110.             if (feature & (1<<gestaltStandardFile58)) hasCustomFile = TRUE;
  111.         if (Gestalt(gestaltFSAttr, &feature) == noErr)
  112.             if (feature & (1<<gestaltHasFSSpecCalls)) hasFSSpecCalls = TRUE;
  113.         if (Gestalt(gestaltAppleEventsAttr, &feature) == noErr)
  114.             if (feature & (1<<gestaltAppleEventsPresent)) hasAppleEvents = TRUE;
  115.         if (Gestalt(gestaltPPCToolboxAttr, &feature) == noErr)
  116.             {
  117.             if (!(feature & gestaltPPCSupportsRealTime))
  118.                 { PPCInit(); Gestalt(gestaltPPCToolboxAttr, &feature); }
  119.             if (Gestalt(gestaltPPCToolboxAttr, &feature) == noErr) hasPPCToolBox = TRUE;
  120.             }
  121.         }
  122.     return(noErr);
  123.     }
  124.  
  125. local void Load_Menus(void)
  126.     {
  127.     SetMenuBar(GetNewMBar(128));
  128.     AddResMenu(GetMHandle(appleID),'DRVR');
  129.     AppleMenu = GetMHandle(appleID);
  130.     FileMenu  = GetMHandle(fileID);
  131.     EditMenu  = GetMHandle(editID);
  132.     BFMenu    = GetMHandle(BF_ID);
  133.     DrawMenuBar();
  134.     }
  135.  
  136. local void Init_Menus(void)
  137.     {
  138.     CheckItem(BFMenu, bm_60, prefs.frametime == 1);
  139.     CheckItem(BFMenu, bm_30, prefs.frametime == 2);
  140.     CheckItem(BFMenu, bm_20, prefs.frametime == 3);
  141.     CheckItem(BFMenu, bm_15, prefs.frametime == 4);
  142.     }
  143.  
  144. local short GetPreferencesFolder(void)
  145.     {
  146.     long feature;
  147.     if (!hasGestalt || Gestalt(gestaltFindFolderAttr, &feature) != noErr)
  148.         return(sysenvirons.sysVRefNum);
  149.     else
  150.         {
  151.         short wdRef, vRefNum = 0;
  152.         long dirID = 0;
  153.         if (FindFolder(kOnSystemDisk, kPreferencesFolderType, kDontCreateFolder,
  154.             &vRefNum, &dirID) != noErr) return(0);
  155.         if (OpenWD(vRefNum, dirID, CREATOR_CODE, &wdRef) != noErr) return(0);
  156.         //fatal("Prefs folder vrefnum %X, dirid %lX, wdref %X", vRefNum, dirID, wdRef);
  157.         return(wdRef);
  158.         }
  159.     }
  160.  
  161. local OSErr BF_Inits(void)
  162.     {
  163.     if (Init_EnvironmentInfo()) return(1);
  164.  
  165.     Load_Menus();            // must do this before we do getlaunchfiles
  166.     // must do this before we read the prefs
  167.     AppleEventInits();
  168.     PrefsFolderWD = GetPreferencesFolder();
  169.     readprefs();            // Don't do this until after Init_EnvironmentInfo();
  170.     Init_Menus();            // Can't use CheckItem until prefs have been read
  171.     
  172.     if (!hasPPCToolBox || !sysenvirons.hasColorQD)
  173.         { StopAlert(alert_systemtoostupid, NULL); return(1); }
  174.     if (FreeMem() < MEMORY_REQUIREMENT) return(1);
  175.     
  176.     if (sysenvirons.systemVersion < 0x700) MyIconSuite = GetResource('SICN', 128);
  177.     else GetIconSuite(&MyIconSuite, 128, 0xFFFFFFFF);
  178.     if (!MyIconSuite) return(1);
  179.     
  180.     init_globals();
  181.  
  182.     if (PPCToolBoxInit()) return(1);
  183.     BFInterfaceInit();
  184.     InitCursor();        // OK, set cursor to arrow now
  185.     return(noErr);
  186.     }
  187.  
  188. local void Run_BF(void)
  189.     {
  190.     openwindow();
  191.     while(!quitting) handle_next_event(NULL,NULL);
  192.     save_prefs();    // make sure any changed prefs are written back
  193.     }
  194.  
  195. local ProcPtr oldExitToShell;
  196.  
  197. local void BF_Quit(void)
  198.     {
  199.     ConnectionInfo *conn = Connections;
  200.     SetCursor(&waitingcursor);            // Quitting now
  201.     NSetTrapAddress(oldExitToShell, _ExitToShell, ToolTrap);
  202.     while (conn)    // Close all the windows and PPC connections, if any
  203.         {
  204.         PPCToolBoxDisconnect(conn);
  205.         BFInterfaceClose(conn);
  206.         conn = conn->next;
  207.         }
  208.     PPCToolBoxQuit();        // Close PPC port
  209.     BFInterfaceQuit();
  210.     SetCursor(&qd.arrow);    // Finished quitting
  211.     }
  212.  
  213. local long MyExitToShell(void)
  214.     {
  215.     asm    {
  216.         move.l    #0x12345678, a5            ; Set A5. MUST come first !
  217.         bsr        BF_Quit
  218.         move.l    oldExitToShell, a0
  219.         jmp        (a0)
  220.         }
  221.     }
  222.  
  223. export void main(void)
  224.     {
  225.     MacInits();
  226.     asm    {    lea        MyExitToShell, a0    ; pointer to jump table entry
  227.             move.l    2(a0), a0            ; real address of routine
  228.             move.l    a5, 2(a0)            ; store A5 there
  229.         }
  230.     oldExitToShell = (void*)NGetTrapAddress(_ExitToShell, ToolTrap);
  231.     NSetTrapAddress(MyExitToShell, _ExitToShell, ToolTrap);
  232.     if (BF_Inits() == noErr) Run_BF();
  233.     ZeroScrap();    // Just for completeness,
  234.     TEToScrap();    // Write TEScrap back to the clipboard before exiting
  235.     BF_Quit();
  236.     }
  237.